<?php
require_once('includes/common.php');
$id = isset($_REQUEST['id'])?$_REQUEST['id']:0;

///////////////////////////////////////////////
if(!empty($_POST)){

  $uname = $_POST['uname'];
  $dob = $_POST['dob'];
  $country = $_POST['country'];

  $updateQuery = "UPDATE `users` SET `uname`='$uname',`dob`='$dob',`country`='$country' WHERE `id`='$id'";
  mysql_query($updateQuery);
  if(mysql_affected_rows() ==  1){
    echo "User updated successfully";
  }
}
///////////////////////////////////////////////

  $selectQuery = "SELECT * FROM `users` WHERE `id`=$id";
  $selectResult = mysql_query($selectQuery) or die(mysql_error());
  $userDetail = mysql_fetch_assoc($selectResult);
////////////////////////////////////////////////

?>
<form action="update_user.php" method="post" name="updateUser">

<input type="hidden" name="id" value="<?=$userDetail['id']?>">

<table cellpadding="10" border="1" width="80%" align="center">
  <thead>
    <tr>
      <th colspan="2">Update User</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td align="right" width="40%">Name :</td>
      <td width="60%"><input type="text" name="uname" value="<?=$userDetail['uname']?>"></td>
    </tr>
    <tr>
      <td align="right" width="40%">Email :</td>
      <td width="60%"><input type="text" name="email" disabled value="<?=$userDetail['email']?>"></td>
    </tr>
    <tr>
      <td align="right" width="40%">Date Of Birth :</td>
      <td width="60%"><input type="date" name="dob" value="<?=$userDetail['dob']?>"></td>
    </tr>
    <tr>
      <td align="right" width="40%">Country :</td>
      <td width="60%">
        <select name="country">
          <option value="">-SELECT-</option>
<?php foreach(getCountries() as $countryCode => $CountryName ) {

  $isSelected = ($countryCode == $userDetail['country']) ? 'selected':'';
?>
        <option <?=$isSelected?> value="<?=$countryCode?>"><?=$CountryName?></option>
<?php }?>
        </select>
        </td>
    </tr>
    <tr>
      <td align="right" width="40%">&nbsp;</td>
      <td width="60%"><input type="submit" name="submit" value="Please update me !!"></td>
    </tr>
  </tbody>
</table>

</form>
